<?php
namespace app\system\model;
/**
 * 系统菜单
 */
class MenuModel {
	/**
     * 获取所有菜单
     */
	public function getMenu($loginUserInfo = array()){
        if(!empty($loginUserInfo)){
            $menuPurview = $loginUserInfo['menu_purview'];
        }
		$list = get_all_service('Menu','System');
		//合并菜单
        foreach ($list as $value) {
            $menu = array_merge_recursive((array)$menu,(array)$value);
        }
        //当前URL
        $cutUrl = url(APP_NAME . '/' . CONTROLLER_NAME . '/index');
        $extName = config('ROUTE.REWRITE_SUFFIX');
        if(strripos($cutUrl, $extName, 0) !== false){
            $cutUrl = substr($cutUrl, 0, -strlen($extName));
        }
        $cutUrl = substr($cutUrl, 0, -6);
        //重置菜单
        $menuList = array();
        foreach ((array) $menu as $topKey => $top) {
            $topCur = false;
            //二级菜单
            $parentMenu = array();
            if (!empty($top['menu']) && is_array($top['menu'])) {
                foreach($top['menu'] as $parentKey => $parent) {
                    $cur = false;
                    //三级菜单
                    $subMenu = array();
                    if (!empty($parent['menu']) && is_array($parent['menu'])) {
                        foreach ($parent['menu'] as $subKey => $sub) {
                            //判断权限
                            $purviewName = $top['name'].'_'.$parent['name'].'_'.$sub['name'];
                            if(in_array($purviewName, (array)$menuPurview) || $loginUserInfo['group_id'] == 1 || !$loginUserInfo){
                                //设置高亮
                                if(strpos($sub['url'],$cutUrl.'/') !== false || strpos($sub['url'],$cutUrl.'.') !== false){
                                    $cur = true;
                                    $sub['cur'] = $cur;
                                }
                                $subMenu['menu'][] = $sub;
                            }
                        }
                    }

                    //二级菜单生成
                    if(!empty($subMenu)){
                        $subMenu['menu'] = array_order($subMenu['menu'], 'order', 'asc');
                        $topMenu = reset($subMenu['menu']);
                        $subMenu['url'] = $topMenu['url'];
                        $subMenu['name'] = $parent['name'];
                        $subMenu['icon'] = $parent['icon'];
                        $subMenu['order'] = $parent['order'];
                        if($cur) {
                            $subMenu['cur'] = true;
                            $topCur = true;
                        }
                        $parentMenu['menu'][] = $subMenu;
                    }
                }
            }
            //顶级菜单生成
            if(!empty($parentMenu)){
                $parentMenu['menu'] = array_order($parentMenu['menu'], 'order', 'asc');
                $parentMenu['name'] = $top['name'];
                $parentMenu['icon'] = $top['icon'];
                $parentMenu['order'] = $top['order'];
                if($topCur) {
                    $parentMenu['cur'] = true;
                }
                $parentInfo = reset(reset(reset($parentMenu['menu'])));
                $parentMenu['url'] = $parentInfo['url'];
                $menuList[$topKey] = $parentMenu;
            }
        }
        //排序顶级菜单
        $menuList = array_order($menuList, 'order', 'asc', true);
        return $menuList;
	}
}